<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form name="form1" method="post" action="">
        <p>数据1： <input type="text" name="data1" size="5"></p>
        <p>数据2： <input type="text" name="data2" size="5"></p>
        <hr />
        <p id="inputBtn">
            <input type="button" value="加">
            <input type="button" value="减">
            <input type="button" value="乘">
            <input type="button" value="除">
            <input type="reset" value="清空">
        </p>
        <p>运算结果： <input type="text" id="result" size="5"></p>
    </form>
</body>
<script>
    function equal() {
        var data1 = document.getElementsByName("data1")[0];
        var data2 = document.getElementsByName("data2")[0];


        // var oInput = document.getElementById('inputBtn').getElementsByTagName('input');
        var oInput = document.getElementsByTagName('input');
        // console.log(oInput);

        var result = document.getElementById('result');
        var total;

        for (var i = 0; i < oInput.length; i++) {
            (function (j) {

                oInput[j].onclick = function () {
                    if (this.type !== 'button') return;
                    // console.log(data1.value);

                    switch (this.value) {

                        case "加": total = parseFloat(data1.value) + parseFloat(data2.value); break;
                        case "减": total = data1.value - data2.value; break;
                        case "乘": total = data1.value * data2.value; break;
                        case "除": total = data1.value / data2.value; break;
                    }

                    // console.log(total);
                    // console.log(data1);
                    result.value = total;
                }
            }(i))
        }
    }
    equal();
</script>

</html>